package com.cskaoyan.wordmemorize.excel;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.cskaoyan.wordmemorize.common.constant.CommonConstant;
import com.cskaoyan.wordmemorize.converter.TopicConverter;
import com.cskaoyan.wordmemorize.dao.entity.ReviewSubTopicDO;
import com.cskaoyan.wordmemorize.dao.entity.ReviewTopicDO;
import com.cskaoyan.wordmemorize.dao.entity.TopicOptionDO;
import com.cskaoyan.wordmemorize.dao.entity.VocDO;
import com.cskaoyan.wordmemorize.dao.mapper.ReviewSubTopicMapper;
import com.cskaoyan.wordmemorize.dao.mapper.ReviewTopicMapper;
import com.cskaoyan.wordmemorize.dao.mapper.TopicOptionsMapper;
import com.cskaoyan.wordmemorize.dao.mapper.VocMapper;
import com.cskaoyan.wordmemorize.dto.admin.FillTopicExcelDTO;
import jakarta.annotation.PostConstruct;
import jakarta.annotation.Resource;
import org.springframework.stereotype.Component;
import org.springframework.transaction.support.TransactionTemplate;

import java.util.List;

@Component
public class FillWordExcelSaver extends AbstractExcelSaver<FillTopicExcelDTO, FillTopicExcelDTO>{


    @Resource
    TransactionTemplate transactionTemplate;

    @Resource
    ReviewTopicMapper reviewTopicMapper;

    @Resource
    TopicConverter topicConverter;

    @Resource
    VocMapper vocMapper;

    @Resource
    TopicOptionsMapper topicOptionsMapper;

    @Resource
    ReviewSubTopicMapper reviewSubTopicMapper;

    @PostConstruct
    public void init() {
        super.init(CommonConstant.BATCH, transactionTemplate);
    }


    @Override
    public void doSave(List<FillTopicExcelDTO> targetList) {

        for (int i = 0; i < targetList.size(); i++) {
            FillTopicExcelDTO fillTopicExcelDTO = targetList.get(i);

            // 保存题目
            ReviewTopicDO reviewTopicDO = new ReviewTopicDO();
            reviewTopicDO.setTopicType(CommonConstant.TOPIC_TYPE_WORD_FILL);
            reviewTopicDO.setVocabulary(fillTopicExcelDTO.getAnswer1() + "," + fillTopicExcelDTO.getAnswer2() + "," + fillTopicExcelDTO.getAnswer3());
            reviewTopicDO.setTopic(fillTopicExcelDTO.getSubTopic1() + "\n" + fillTopicExcelDTO.getSubTopic2() + "\n" + fillTopicExcelDTO.getSubTopic3());
            reviewTopicMapper.insert(reviewTopicDO);

            // 保存小题
            saveSubReviewTopic(fillTopicExcelDTO.getSubTopic1(), fillTopicExcelDTO.getAnswer1(), reviewTopicDO.getId(), 1);
            saveSubReviewTopic(fillTopicExcelDTO.getSubTopic2(), fillTopicExcelDTO.getAnswer2(), reviewTopicDO.getId(), 2);
            saveSubReviewTopic(fillTopicExcelDTO.getSubTopic3(), fillTopicExcelDTO.getAnswer3(), reviewTopicDO.getId(), 3);

            // 保存选项
            TopicOptionDO topicOptionDO = new TopicOptionDO();
            topicOptionDO.setTid(reviewTopicDO.getId());
            topicOptionDO.setItem(fillTopicExcelDTO.getError());
            topicOptionDO.setIsAnswer(0);
            topicOptionsMapper.insert(topicOptionDO);
        }

    }

    private void saveSubReviewTopic(String topic, String answer, Long tid, Integer subTopicNumber) {
        LambdaQueryWrapper<VocDO> first = new LambdaQueryWrapper<VocDO>()
                .eq(VocDO::getContent, answer)
                .select(VocDO::getId);
        VocDO vocDO = vocMapper.selectOne(first);

        ReviewSubTopicDO reviewSubTopicDO = new ReviewSubTopicDO();
        reviewSubTopicDO.setTid(tid);
        reviewSubTopicDO.setTopicItem(topic);
        reviewSubTopicDO.setVocId(vocDO.getId());
        reviewSubTopicDO.setVocabulary(answer);
        reviewSubTopicDO.setSubTopicNumber(subTopicNumber);
        reviewSubTopicMapper.insert(reviewSubTopicDO);
    }

}
